<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\user_center\logic\user;
use think\facade\Hook;
use think\DB;

class Memberlogic{

	/**
     * 关注用户
     * @param  int $uid  			用户ID
     * @param  int $follow_uid    	被关注者id
     * @param  int $from_type    	关注渠道
     * @param  int $follow_type  	关注行为  1关注 2取关
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
    public function focus_user($uid, $follow_uid, $from_type = 0, $follow_type){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'uid'		 	=> $uid,
                'follow_uid'   	=> $follow_uid,
                'from_type'   	=> $from_type,
                'follow_type' 	=> $follow_type,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
			if($follow_type == 1){
				//关注
				$result = model('UcMemberFollow', 'model\user') -> addData($uid, $follow_uid, $from_type);
			}
			if($follow_type == 2){
				//取关
				$result = model('UcMemberFollow', 'model\user') -> deleteData($uid, $follow_uid);
			}
			if($result['code'] < 0){
				DB::rollback();
				return array_out(-1005, 'error');//业务逻辑错误-1005
			}
			
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    	
    }
	
	/**
     * 绑定手机号码
     * @param  int $mobile      手机号码
     * @param  int $code  	    验证码
     * @param  int $bind_type   1 微信 2 苹果id
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function bind_mobile($register_id, $mobile, $code, $mobile_type = 3, $bind_type){
		DB::startTrans();//启动事务
		try{
            $params = [
                'register_id'   => $register_id,
                'mobile'   		=> $mobile,
                'code' 			=> $code,
                'mobile_type'	=> $mobile_type,
                'bind_type'	    => $bind_type,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/
            if($bind_type == 1){//微信
                $model_name = 'UcMemberWechat';
                $result = model('UcMemberWechat', 'model\user') -> where('unionid', $register_id) -> find();
            }
            if($bind_type == 2){//苹果id
                $model_name = 'UcMemberApple';
                $result = model('UcMemberApple', 'model\user') -> where('apple_id', $register_id) -> find();
            }
            if(empty($result)){
                DB::rollback();
                return array_out(-1005, 'error1');
            }
            $bind_id = $result -> id;

            // 检测手机号是否 已经注册过了
            $map = [];
            $map['mobile'] = $mobile;
            $mobile_info = model('UcMemberMobile', 'model\user') -> where($map) -> find();
            if(empty($mobile_info)){ //新用户注册
                //注册用户信息
                $nick_name  = $result -> nick_name;
                $avatar_url = $result -> avatar_url;
                $result = model('Member', 'model\user') -> addUser($nick_name, 1, '暂无签名', $avatar_url, date('Y-m-d'));
                if($result['code'] < 0){
                    DB::rollback();
                    return array_out(-1005, 'error2'); // 注册失败
                }
                $uid = $result['data'];
                //1. 绑定手机号
                $result = model('UcMemberMobile', 'model\user') -> addData($uid, $mobile);
                if($result['code'] < 0){
                    DB::rollback();
                    return array_out(-1005, 'error3');
                }
            }else{ //老用户绑定
                $uid = $mobile_info -> uid;
            }
            //2. 绑定微信号
            $data = [];
            $data['uid'] = $uid;
            $map = [];
            $map['id'] = $bind_id;
            $res = model($model_name, 'model\user') ->update($data, $map);
            if(!$res){
                DB::rollback();
                return array_out(-1005, 'error4');
            }
            // 注册发放优惠券
            auto_receive_coupon($uid, 0);

            $result5 = controller('Userlogic', 'logic\user') -> getUserinfoByUserInfo($uid);//获取用户详情
            if($result5['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error5');
            }

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			return array_out(200, 'success', $result5['data']);
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}

	/**
     * 校验旧手机号码以及生成token
     * @param  int $uid  	用户ID
     * @param  int $mobile  手机号码
     * @param  int $code    验证码
	 * @param  int $mobile_type 验证码类型
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function change_old_mobile($uid, $mobile , $code, $mobile_type = 2){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'			=> $uid,
                'mobile'   		=> $mobile,
                'code'   		=> $code,
                'mobile_type'   => $mobile_type,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/
			
			//验证码
			$result = model('UcMemberMobileSendcode', 'model\user') -> getDataByMobile($mobile, $mobile_type, 0);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, '验证码已失效');
            }

            //更新token 及 状态
            $send_id = $result['data']->id;
            $token = mboile_old_token($uid, $mobile, $code);
            $map = [];
            $map['id'] = $send_id;
            $data = [];
            $data['mobile_token']	= $token['mobile_token'];
            $data['mobile_status']	= 2;//dump($map);
            $result = model('UcMemberMobileSendcode', 'model\user') -> update($data, $map);
			if(!$result){
                DB::rollback();
                return array_out(-1005, 'error1');
            }
                
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			$rel['mobileToken'] = (string)$token['mobile_token'];
			return array_out(200, 'success', $rel);
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	/**
     * 新手机号码绑定
     * @param  int $uid  	用户ID
     * @param  int $mobile  手机号码
     * @param  int $code    验证码
	 * @param  int $mobile_type 	验证码类型
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function change_new_mobile($uid , $mobile , $code, $mobile_token, $mobile_type = 4){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'				=> $uid,
                'mobile'   			=> $mobile,
                'code'   			=> $code,
                'mobile_token'  	=> $mobile_token,
                'mobile_type'   	=> $mobile_type,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/
			
			//变更手机号码
			$result = model('UcMemberMobile', 'model\user') -> updateDataByMobile($uid, $mobile, $mobile_token);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	
	/**
     * 绑定微信
     * @param  int 	  $uid  	用户ID
     * @param  string $openid  
     * @param  string $unionid 
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function bind_wechat($uid, $openid, $unionid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		=> $uid,
                'openid'   	=> $openid,
                'unionid' 	=> $unionid,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

			//新增绑定
			$result = model('UcMemberWechat', 'model\user') -> addData($uid, $openid, $unionid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	/**
     * 解除绑定微信
     * @param  int 	  $uid  	用户ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function unbind_wechat($uid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		=> $uid,
            ];
			
			/********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

			// 解除绑定
			$result = model('UcMemberWechat', 'model\user') -> deleteData($uid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	/**
     * 绑定微信
     * @param  int 	  $uid  	用户ID
     * @param  string $apple_id  
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function bind_apple($uid, $apple_id){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		=> $uid,
                'apple_id'  => $apple_id,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

			//新增绑定
			$result = model('UcMemberApple', 'model\user') -> addData($uid, $apple_id);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	/**
     * 解除绑定微信
     * @param  int 	  $uid  	用户ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function unbind_apple($uid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		=> $uid,
            ];
			
			/********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

			// 解除绑定
			$result = model('UcMemberApple', 'model\user') -> deleteData($uid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	/**
     * 头像上传
     * @param  int 		$uid  		用户ID
     * @param  string 	$oss_url  	头像oos路径
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function avatar_upload($uid, $oss_url){
		try{
            $params = [
                'uid'			=> $uid,
                'oss_url'   	=> $oss_url,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
            return array_out(200, 'success');
        } catch (\Exception $e) {
            return array_out(-9999, $e -> getMessage());
        }
	}
	/**
     * 更新用户信息
     * @param  int 		$uid  		用户ID
     * @param  string 	$nick_name  	昵称
     * @param  int 		$sex 		性别
     * @param  data 	$birthday 	生日
     * @param  string 	$avatar_url 	头像
     * @param  string 	$autograph 	个性签名
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function edit_profile($uid, $nick_name, $sex, $birthday, $autograph, $avatar_url, $title, $member_status = -1){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'			=> $uid,
                'nick_name'   	=> $nick_name,
                'sex' 			=> $sex,
                'birthday' 		=> $birthday,
                'autograph' 	=> $autograph,
                'avatar_url' 	=> $avatar_url,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

			//更新用户信息
			$result = model('Member', 'model\user') -> updateDataByUser($uid, $nick_name, $sex, $autograph, $avatar_url, $birthday, $member_status, $title);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
			DB::rollback(); 
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	
	/**
     * 退出登录
     * @param  int $uid  		用户ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function login_out($uid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid' => $uid,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            //清空登录信息
			$result = model('UcMemberAuthtoken', 'model\user') -> WipeDataByUid($uid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	
	/**
     * 粉丝列表
     * @param  int $uid  		用户ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function fans_list($user_id, $user_uid, $user_name){
		try{
            $params = [
                'user_id'   => $user_id,
                'user_uid'  => $user_uid,
                'user_name'  => $user_name,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/
			
			$result = model('UcMemberFollow', 'model\user') -> getListFansByUid($user_uid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			$res = self::getFansByFollow($user_id, $result['data']);
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			return array_out(200, 'success', $res);
        } catch (\Exception $e) {
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	/**
     * 粉丝列表通知
     * @param  int $uid  		用户ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function fans_notice_list($user_id, $user_uid, $user_name){
		try{
            $params = [
                'user_id'   => $user_id,
                'user_uid'  => $user_uid,
                'user_name'  => $user_name,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/
			
			$result = model('UcMemberFollow', 'model\user') -> getListFansByUid($user_uid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			$res = self::getFansByFollow($user_id, $result['data'], 1);
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			return array_out(200, 'success', $res);
        } catch (\Exception $e) {
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	/**
     * 关注列表
     * @param  int $uid  		用户ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function follow_list($user_id, $user_uid){
		try{
            $params = [
                'user_id'   => $user_id,
                'user_uid'  => $user_uid,
            ];
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            $result = model('UcMemberFollow', 'model\user') -> getListFollowByUid($user_uid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
			$res = self::getFansByFollow($user_id, $result['data']);
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			return array_out(200, 'success', $res);
        } catch (\Exception $e) {
            return array_out(-9999, $e -> getMessage());
        }
    }
    
    /**
     * 用户反馈
     * @param  int 	  $uid  	用户ID
     * @param  string $content 反馈内容  
     * @author baoshi
     * @return array_out
     */
	public function add_feedback($uid, $content){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		    => $uid,
                'content'   	=> $content,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $rel = model('UcMemberFeedback', 'model\user') -> addData($uid, $content);
            if($rel['code'] != 200){
                DB::rollback();
                return array_out(-1005, 'error1');//前置错误 -1002
            }
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
    }
	
	/**
     * 获取用户认证信息
     * @param  int 	  $uid  	用户ID
     * @author shiqiang
     * @return array_out
     */
	public function get_titlecertification($uid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		    => $uid
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $info = model('UcMemberTitlecertification', 'model\user') -> where('uid', $uid) -> find();
			
			$result = [];
			if(!empty($info)){
				$result['certifi_type']   				= $info['certifi_type'];
				$result['classification_id']  			= $info['classification_id'];
				$result['business_license_url']   		= $info['business_license_url'];
				$result['certification_letter_url']   	= $info['certification_letter_url'];
				$result['other_url1']  					= $info['other_url1'];
				$result['other_url2']   				= $info['other_url2'];
				$result['other_url3']   				= $info['other_url3'];
				$result['other_url4']   				= $info['other_url4'];
				$result['other_url5']   				= $info['other_url5'];
				$result['certifi_name']   				= $info['certifi_name'];
				$result['certification_information']   	= $info['certification_information'];
				$result['operator_name']   				= $info['operator_name'];
				$result['operator_mobile']   			= $info['operator_mobile'];
				$result['operator_email']   			= $info['operator_email'];
				$result['invoice_email']   				= $info['invoice_email'];
				$result['yaoqing_code']   				= $info['yaoqing_code'];
				$result['opening_bank']   				= $info['opening_bank'];
				$result['bank_code']   					= $info['bank_code'];
				$result['address']   					= $info['address'];
				$result['tel_num']   					= $info['tel_num'];
				$result['status']   					= $info['status'];
				$result['reason']   					= $info['reason'];
				$result['id_card_positive']   			= $info['id_card_positive'];
				$result['id_card_side']   				= $info['id_card_side'];
			}
			
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success', (array)$result);
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
    }
	
	/**
     * 用户头衔认证
     * @param  int 	  	$uid  	用户ID
     * @param  int 		$type   1企业 2个人
     * @param  int 		$classification_id   		行业id
     * @param  string	$business_license_url		营业执照url 
     * @param  string	$certification_letter_url	认证公函url 
     * @param  string	$other_url1-5				其他资质
     * @param  string	$name						申请者名
     * @param  string	$certification_information	认证信息
     * @param  string	$operator_name				管理员名
     * @param  int		$operator_mobile			手机号码
     * @param  int		$operator_code				验证码
     * @param  string	$operator_email				管理员邮箱
     * @param  string	$invoice_email				发票接收邮箱
     * @param  string	$yaoqing_code				邀请码
     * @param  string	$opening_bank				开户行
     * @param  string	$bank_code					银行卡号
     * @param  string	$address					企业地址
     * @param  string	$tel_num					企业电话
     * @author shiqiang
     * @return array_out
     */
	public function add_titlecertification($uid, $certifi_type, $classification_id, $business_license_url, $certification_letter_url, $other_url1, $other_url2, $other_url3, $other_url4, $other_url5, $certifi_name, $certification_information, $operator_name, $operator_mobile, $operator_code, $operator_email, $invoice_email, $yaoqing_code, $opening_bank, $bank_code, $address, $tel_num, $id_card_positive, $id_card_side, $mobile_type = 5){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		    				=> $uid,
                'certifi_type'   				=> $certifi_type,
                'classification_id'   			=> $classification_id,
                'business_license_url'   		=> $business_license_url,
                'certification_letter_url'   	=> $certification_letter_url,
                'other_url1'   					=> $other_url1,
                'other_url2'   					=> $other_url2,
                'other_url3'   					=> $other_url3,
                'other_url4'   					=> $other_url4,
                'other_url5'   					=> $other_url5,
                'certifi_name'   				=> $certifi_name,
                'certification_information'   	=> $certification_information,
                'operator_name'   				=> $operator_name,
                'operator_mobile'   			=> $operator_mobile,
                'operator_code'   				=> $operator_code,
                'operator_email'   				=> $operator_email,
                'invoice_email'   				=> $invoice_email,
                'yaoqing_code'   				=> $yaoqing_code,
                'opening_bank'   				=> $opening_bank,
                'bank_code'   					=> $bank_code,
                'address'   					=> $address,
                'tel_num'   					=> $tel_num,
                'id_card_positive'   			=> $id_card_positive,
                'id_card_side'   				=> $id_card_side,
                'mobile_type'   				=> $mobile_type,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/
			
            /********业务逻辑********/
			$info = model('UcMemberTitlecertification', 'model\user') -> where('uid', $uid) -> find();
			
			if(!empty($info) && $info -> getData('status') == 1){
				return array_out(-1005, '已通过申请,无需再次提交');
			}
			if(!empty($info)){//编辑认证信息
				$rel = model('UcMemberTitlecertification', 'model\user') -> editData($params);
			}else{//添加认证信息
				$rel = model('UcMemberTitlecertification', 'model\user') -> addData($params);
			}
			
            if($rel['code'] != 200){
                DB::rollback();
                return array_out(-1005, 'error1');
            }
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
    
    /**
     * 用户反馈
     * @param  int 	  $uid  	用户ID
     * @param  string $content 反馈内容  
     * @author baoshi
     * @return array_out
     */
	public function delete_focus_user($uid, $fans_user_id){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		        => $uid,
                'fans_user_id'   	=> $fans_user_id,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $rel = model('UcMemberFollow', 'model\user') -> deleteData($fans_user_id, $uid);//移除粉丝
            if($rel['code'] <= 0){
                DB::rollback();
                return array_out(-1005, 'error');//逻辑错误
            }
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
    }

    /**
     * 隐私设置
     * @param  int 	  $uid  	用户ID
     * @param  int $fans_follow_auth 隐私设置 谁可以看我得关注和粉丝列表 0所有人 1仅自己
     * @author baoshi
     * @return array_out
     */
	public function edit_privacy($uid, $fans_follow_auth){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		        => $uid,
                'fans_follow_auth'   	=> $fans_follow_auth,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $rel = model('UcMemberPrivacy', 'model\user') -> updateDataByUid($uid, $fans_follow_auth);
            if($rel['code'] <= 0){
                DB::rollback();
                return array_out(-1005, 'error');//逻辑错误
            }
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
    }
	
	/**
     * 用户黑名单列表
     * @param  int 	  $uid  	用户ID
     * @author shiqiang
     * @return array_out
     */
	public function user_black_list($uid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid' => $uid,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $rel = model('UcMemberBlackList', 'model\user') -> getDataByUid($uid);
			$result_list = [];
			foreach($rel['data'] as $value){
				$result = controller('user_center/Userlogic', 'logic\user') -> get_user_info($uid, $value -> black_uid);
				if($result['code'] < 0) continue;
				$result_list[] = $result['data'];
			}
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success', $result_list);
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	/**
     * 拉黑用户
     * @param  int 	  $user_id  	用户ID
     * @param  int 	  $black_uid  	拉黑用户ID
     * @author shiqiang
     * @return array_out
     */
	public function add_user_black($user_id, $black_uid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'user_id' => $user_id,
                'black_uid' => $black_uid,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $rel = model('UcMemberBlackList', 'model\user') -> addData($user_id, $black_uid);
			if($rel['code'] < 0){
				DB::rollback();
				return array_out(-1005, 'error');
			}
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	/**
     * 取消拉黑
     * @param  int 	  $user_id  	用户ID
     * @param  int 	  $black_uid  	拉黑用户ID
     * @author shiqiang
     * @return array_out
     */
	public function cancel_user_black($user_id, $black_uid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'user_id' => $user_id,
                'black_uid' => $black_uid,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $rel = model('UcMemberBlackList', 'model\user') -> deleteData($user_id, $black_uid);
			if($rel['code'] < 0){
				DB::rollback();
				return array_out(-1005, 'error');
			}
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	/**
     * 用户搜索
     * @param  int 	  $user_id  	用户ID
     * @param  int 	  $user_name  	用户名(模糊搜索)
     * @author shiqiang
     * @return array_out
     */
	public function get_user_list_search($user_id, $user_name){
		DB::startTrans();//启动事务
		try{
            $params = [
                'user_id' 	=> $user_id,
                'user_name' => $user_name,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
			$page       = input('page');
            $page_size  = input('page_size');
			
            $list_data = datacache('user_center/Member', 'model\user') -> getListByUserNameSearch($user_id, $user_name);
            $return_list = [];
			if(!empty($list_data)){
				$count = count($list_data);//总条数
				$start=($page-1)*$page_size;//偏移量，当前页-1乘以ssss每页显示条数
				$rel_list = array_slice($list_data,$start,$page_size);
				foreach($rel_list as $key => $value){
					$follow_uid = $value['uid'];
					$map = [];
					$map['uid'] = $follow_uid;
					$map['follow_uid'] = $user_id;
					$other_info = model('user_center/UcMemberFollow', 'model\user') -> where($map) -> find();//是否关注了我
					$rel_list[$key]['other'] = !empty($other_info) ? 1 : 0;
				}
				$return_list = self::getFansByFollow($user_id, $rel_list);
			}
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success', $return_list);
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	//重组我的关注/粉丝数据
	private function getFansByFollow($uid, $result, $type = 0){
		$res = [];
		foreach($result as $key => $value){
            $userinfo = controller('user_center/Userlogic', 'logic\user') -> get_user_info($uid, $value['uid']);
            $info['userInfo'] 	= $userinfo['data'];
            $info['other'] 		= $value['other'] > 0 ? 1 : 0;

			if($type == 1){
				$info['createTime'] 	= (int)$value['create_time'];
				
				//$from_type 			 = $userinfo['data']['nickName'];
				$from_type			=  $value['from_type'] == '' ? '关注了您' : '通过' . $value['from_type'] . '关注了您';
				$info['fromType'] 	 = (string)$from_type;
			}
			$res[] = $info;
		}
		return $res;
	}
	
	
	
	/**
     * 申请成为团长
     * @param int $uid    			用户id
     * @param string $username  	用户名
     * @param int $mobile  			手机号
     * @param string $city  		城市
     * @param string $attributes  	个人属性
     * @param string $address  		自提点地址
     * @param string $experience  	经验
     * @param string $wechat_group  是否有周边微信群
     * @param string $wechat_num  	群人数
     * @author shiqiang
     * @return array_out
     */
	public function apply_team_leader($user_id, $username, $mobile, $city, $attributes, $address, $experience, $wechat_group, $wechat_num){
		DB::startTrans();//启动事务
		try{
            $params = [
                'user_id' 		=> $user_id,
                'username' 		=> $username,
                'mobile' 		=> $mobile,
                'city' 			=> $city,
                'attributes' 	=> $attributes,
                'address' 		=> $address,
                'experience' 	=> $experience,
                'wechat_group' 	=> $wechat_group,
                'wechat_num' 	=> $wechat_num,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
			$result = model('UcApplyTeamLeader', 'model\user') -> addData($user_id, $username, $mobile, $city, $attributes, $address, $experience, $wechat_group, $wechat_num);
			
			if($result['code'] < 0){
				DB::rollback();
				return array_out(-1005, 'error');//业务逻辑错误-1005
			}	
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	/**
     * 申请供应商
     * @param int $uid    			用户id
     * @param string $username  	用户名
     * @param int $mobile  			手机号
     * @param string $corporate_name 供应商名字
     * @param string $brand  		 品牌
     * @param string $supplier_type  供应商类型
     * @param string $city  	     城市
     * @param string $class_temp     可提供产品
     * @author shiqiang
     * @return array_out
     */
	public function apply_supplier($user_id, $username, $mobile, $corporate_name, $brand, $supplier_type, $city, $class_temp){
		DB::startTrans();//启动事务
		try{
            $params = [
                'user_id' 			=> $user_id,
                'username' 			=> $username,
                'mobile' 			=> $mobile,
                'corporate_name' 	=> $corporate_name,
                'brand' 			=> $brand,
                'supplier_type' 	=> $supplier_type,
                'city' 				=> $city,
                'class_temp' 		=> $class_temp,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
			$result = model('UcApplySupplier', 'model\user') -> addData($user_id, $username, $mobile, $corporate_name, $brand, $supplier_type, $city, $class_temp);
			
			if($result['code'] < 0){
				DB::rollback();
				return array_out(-1005, 'error');//业务逻辑错误-1005
			}	
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
}